package jnpf.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.service.BillRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.base.UserInfo;
import jnpf.base.vo.DownloadVO;
import jnpf.config.ConfigValueUtil;
import jnpf.base.util.VisualUtils;
import jnpf.exception.DataException;

import org.springframework.transaction.annotation.Transactional;
import jnpf.base.entity.ProvinceEntity;
import jnpf.engine.entity.FlowTaskEntity;
import jnpf.engine.enums.FlowStatusEnum;
import jnpf.engine.service.FlowTaskService;
import jnpf.exception.WorkFlowException;
import jnpf.model.zxgl_xiangmurenwu.*;
import jnpf.model.zxgl_xiangmurenwu.ZXGL_XiangMuRenWuPagination;
import jnpf.entity.*;
import jnpf.base.service.ProvinceService;
import jnpf.base.service.VisualdevService;
import jnpf.entity.ZXGL_XiangMuRenWuMXEntity;
import jnpf.util.*;
import jnpf.base.util.*;
import jnpf.base.vo.ListVO;
import jnpf.util.context.SpringContext;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import jnpf.generater.util.GeneraterSwapUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import jnpf.base.entity.VisualdevEntity;
import jnpf.entity.ZXGL_XiangMuRenWuEntity;
import jnpf.service.ZXGL_XiangMuRenWuService;
import jnpf.entity.ZXGL_XiangMuRenWuMXEntity;
import jnpf.service.ZXGL_XiangMuRenWuMXService;
import jnpf.generater.util.GeneraterSwapUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import jnpf.file.FileApi;
import jnpf.model.upload.UploadFileModel;
import jnpf.emnus.FileTypeEnum;

import java.io.ByteArrayOutputStream;

import jnpf.provider.file.FileUploadProvider;
import org.apache.dubbo.config.annotation.DubboReference;


/**
 * ZXGL_XiangMuRenWu
 *
 * @版本： V2.0
 * @版权： 河北交投智能交通技术有限责任公司
 * @作者： 数字交投项目组
 * @日期： 2025-03-08
 */
@Slf4j
@RestController
@Api(tags = "ZXGL_XiangMuRenWu", value = "zxgl")
@RequestMapping("/ZXGL_XiangMuRenWu")
public class ZXGL_XiangMuRenWuController {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;

    @Autowired
    private VisualdevService visualdevService;

    @Autowired
    private FlowTaskService flowTaskService;

    @Autowired
    private ZXGL_XiangMuRenWuService zXGL_XiangMuRenWuService;

    @Autowired
    private ZXGL_XiangMuRenWuMXService zXGL_XiangMuRenWuMXService;


    /**
     * 列表
     *
     * @param zXGL_XiangMuRenWuPagination
     * @return
     */
    @PostMapping("/getList")
    public ActionResult list(@RequestBody ZXGL_XiangMuRenWuPagination zXGL_XiangMuRenWuPagination) throws IOException {
        List<ZXGL_XiangMuRenWuEntity> list = zXGL_XiangMuRenWuService.getList(zXGL_XiangMuRenWuPagination);
        //处理id字段转名称，若无需转或者为空可删除
        for (ZXGL_XiangMuRenWuEntity entity : list) {
            entity.setXiangmuzerenren(generaterSwapUtil.userSelectValues(entity.getXiangmuzerenren()));
            entity.setXiangmuzerenbumen(generaterSwapUtil.comSelectValues(entity.getXiangmuzerenbumen()));
        }
        List<ZXGL_XiangMuRenWuListVO> listVO = JsonUtil.getJsonToList(list, ZXGL_XiangMuRenWuListVO.class);
        for (ZXGL_XiangMuRenWuListVO zXGL_XiangMuRenWuVO : listVO) {
        }


        for (ZXGL_XiangMuRenWuListVO vo : listVO) {
            FlowTaskEntity flowTaskEntity = flowTaskService.getInfoSubmit(vo.getId());
            if (flowTaskEntity != null) {
                vo.setFlowState(flowTaskEntity.getStatus());
            } else {
                vo.setFlowState(null);
            }
        }
        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = JsonUtil.getJsonToBean(zXGL_XiangMuRenWuPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }


    /**
     * 创建
     *
     * @param zXGL_XiangMuRenWuCrForm
     * @return
     */
    @PostMapping
    @Transactional
    public ActionResult create(@RequestBody @Valid ZXGL_XiangMuRenWuCrForm zXGL_XiangMuRenWuCrForm) throws DataException {
        String visualId = "667355039525772485";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        String mainId = RandomUtil.uuId();
        UserInfo userInfo = userProvider.get();
        ZXGL_XiangMuRenWuEntity entity = JsonUtil.getJsonToBean(zXGL_XiangMuRenWuCrForm, ZXGL_XiangMuRenWuEntity.class);
        entity.setId(mainId);
        entity.setCreatorTime(new Date());
        zXGL_XiangMuRenWuService.save(entity);
        List<ZXGL_XiangMuRenWuMXEntity> ZXGL_XiangMuRenWuMXList = JsonUtil.getJsonToList(zXGL_XiangMuRenWuCrForm.getZxgl_xiangmurenwumxList(), ZXGL_XiangMuRenWuMXEntity.class);

        for (ZXGL_XiangMuRenWuMXEntity entitys : ZXGL_XiangMuRenWuMXList) {
            entitys.setId(RandomUtil.uuId());
            entitys.setId(entity.getMainid());
            zXGL_XiangMuRenWuMXService.save(entitys);
        }
        if (FlowStatusEnum.submit.getMessage().equals(zXGL_XiangMuRenWuCrForm.getStatus())) {
            try {
                VisualUtils.submitFlowTask(visualdevEntity, mainId, zXGL_XiangMuRenWuCrForm, userInfo, zXGL_XiangMuRenWuCrForm.getCandidateList());

            } catch (WorkFlowException e) {
                e.printStackTrace();
            }
            return ActionResult.success("提交成功，请耐心等待");
        }
        return ActionResult.success("保存成功");
    }


    /**
     * 信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ActionResult<ZXGL_XiangMuRenWuInfoVO> info(@PathVariable("id") String id) {
        ZXGL_XiangMuRenWuEntity entity = zXGL_XiangMuRenWuService.getInfo(id);
        ZXGL_XiangMuRenWuInfoVO vo = JsonUtil.getJsonToBean(entity, ZXGL_XiangMuRenWuInfoVO.class);

        //子表
        List<ZXGL_XiangMuRenWuMXEntity> ZXGL_XiangMuRenWuMXList = zXGL_XiangMuRenWuService.GetZXGL_XiangMuRenWuMXList(id);
        for (ZXGL_XiangMuRenWuMXEntity zxgl_xiangmurenwumxEntity : ZXGL_XiangMuRenWuMXList) {

        }
        vo.setZXGL_XiangMuRenWuMXList(JsonUtil.getJsonToList(ZXGL_XiangMuRenWuMXList, ZXGL_XiangMuRenWuMXModel.class));
        //副表
        return ActionResult.success(vo);
    }


    /**
     * 更新
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @Transactional
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ZXGL_XiangMuRenWuUpForm zXGL_XiangMuRenWuUpForm) throws DataException {
        String visualId = "667355039525772485";
        VisualdevEntity visualdevEntity = visualdevService.getInfo(visualId);
        UserInfo userInfo = userProvider.get();
        ZXGL_XiangMuRenWuEntity entity = zXGL_XiangMuRenWuService.getInfo(id);
        if (entity != null) {
            ZXGL_XiangMuRenWuEntity subentity = JsonUtil.getJsonToBean(zXGL_XiangMuRenWuUpForm, ZXGL_XiangMuRenWuEntity.class);
            zXGL_XiangMuRenWuService.update(id, subentity);

            QueryWrapper<ZXGL_XiangMuRenWuMXEntity> ZXGL_XiangMuRenWuMXqueryWrapper = new QueryWrapper<>();
            ZXGL_XiangMuRenWuMXqueryWrapper.lambda().eq(ZXGL_XiangMuRenWuMXEntity::getId, entity.getMainid());
            zXGL_XiangMuRenWuMXService.remove(ZXGL_XiangMuRenWuMXqueryWrapper);

            List<ZXGL_XiangMuRenWuMXEntity> ZXGL_XiangMuRenWuMXList = JsonUtil.getJsonToList(zXGL_XiangMuRenWuUpForm.getZxgl_xiangmurenwumxList(), ZXGL_XiangMuRenWuMXEntity.class);
            for (ZXGL_XiangMuRenWuMXEntity entitys : ZXGL_XiangMuRenWuMXList) {
                entitys.setId(RandomUtil.uuId());
                entitys.setId(entity.getMainid());
                zXGL_XiangMuRenWuMXService.save(entitys);
            }


            if (FlowStatusEnum.submit.getMessage().equals(zXGL_XiangMuRenWuUpForm.getStatus())) {
                try {
                    VisualUtils.submitFlowTask(visualdevEntity, id, subentity, userInfo, zXGL_XiangMuRenWuUpForm.getCandidateList());
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
                return ActionResult.success("提交成功，请耐心等待");
            }
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败，数据不存在");
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        ZXGL_XiangMuRenWuEntity entity = zXGL_XiangMuRenWuService.getInfo(id);
        if (entity != null) {
            FlowTaskEntity taskEntity = flowTaskService.getInfoSubmit(id);
            if (taskEntity != null) {
                try {
                    flowTaskService.delete(taskEntity);
                } catch (WorkFlowException e) {
                    e.printStackTrace();
                }
            }
            zXGL_XiangMuRenWuService.delete(entity);
            QueryWrapper<ZXGL_XiangMuRenWuMXEntity> queryWrapperZXGL_XiangMuRenWuMX = new QueryWrapper<>();
            queryWrapperZXGL_XiangMuRenWuMX.lambda().eq(ZXGL_XiangMuRenWuMXEntity::getId, entity.getMainid());
            zXGL_XiangMuRenWuMXService.remove(queryWrapperZXGL_XiangMuRenWuMX);

        }
        return ActionResult.success("删除成功");
    }


}
